package cn.autumnorange.app.common.provider.rpc;

import cn.autumnorange.app.common.rpc.RPCWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class ProviderRPCServiceImpl implements ProviderRPCService {
  @Value("${server.port}")
  private int port;

  @Value("${spring.application.name}")
  private String providerName;

  public String getRPCIDByHeader() {
    return RPCWrapper.getHttpServletRequestRPCWrapper().getHeader(ProviderRPCService.RPCID);
  }

  public long getRPCLockTimeByHeader() {
    return Long.parseLong(
        RPCWrapper.getHttpServletRequestRPCWrapper().getHeader(ProviderRPCService.RPCLOCKTIME));
  }

  @Override
  public long getConsumerReadTimeout() {
    return Long.parseLong(
        RPCWrapper.getHttpServletRequestRPCWrapper()
            .getHeader(ProviderRPCService.CONSUMERREADTIMEOUT));
  }

  @Override
  public void setProviderId() {
    String providerId = getProviderId();
    if (StringUtils.trimToNull(providerId) != null) {

    } else {
      //      RPCWrapper.getHttpServletRequestRPCWrapper()
      //          .setHeader(ConsumerRPCService.RPCTIMEOUT, String.valueOf(timeOut));
      //      RequestContextHolder.setRequestAttributes(ProviderRPCService.PROVIDERID, providerName
      // + port);
    }
  }

  @Override
  public String getProviderId() {
    return RPCWrapper.getHttpServletRequestRPCWrapper().getHeader(ProviderRPCService.PROVIDERID);
  }
}
